//
//  MyOrderListViewController.m
//  Free
//
//  Created by 张炜健 on 2018/5/16.
//  Copyright © 2018年 张炜健. All rights reserved.
//

#import "MyOrderListViewController.h"
#import "MyOrderViewController.h"
#import "FilesHeader.h"
@interface MyOrderListViewController ()<UIScrollViewDelegate,UITextFieldDelegate>

@property(nonatomic,strong)UIView *indicatorView;
@property(nonatomic,strong)UIButton *currentSeletedButton;
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIView *titleView;

@end

@implementation MyOrderListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self setUpTopTitlesViews];
    [self addSubControlelr];
    [self setContentView];
    [self titleClick:self.titleView.subviews[_flag]];
//    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)setUI{
    [self setNavBackgroundWhiteColorTitleText:@"我的订单"];
}
#pragma mark -- 设置顶部标签栏
-(void)setUpTopTitlesViews
{
    UIView *titlesView = [[UIView alloc]initWithFrame:CGRectMake(0, Height_NavBar, KScreenWidth, 44)];
    
    titlesView.backgroundColor = [UIColor whiteColor];
    // titlesView.alpha = 0.9;
    [self.view addSubview:titlesView];
    self.titleView = titlesView;
    // 底部的条状指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = rgb(255, 111, 3);
    indicatorView.height = 2;
    indicatorView.yv = titlesView.height - indicatorView.height;
    [titlesView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    
    // 内部子标签
    NSArray *titles = @[@"所有订单",@"待支付",@"待发货",@"待收货",@"待评价"];
    
    for (NSInteger i = 0; i < titles.count; i++) {
        
        UIButton *button = [[UIButton alloc]init];
        button.tag = i;
        button.height = titlesView.height;
        button.width = KScreenWidth/ titles.count;
        button.xv  = i*button.width;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:rgb(58, 58, 58) forState:UIControlStateNormal];
        [button setTitleColor:rgb(255, 111, 3) forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        
        if (i == 0) {
            button.enabled = NO;
            self.currentSeletedButton = button;
            [self.currentSeletedButton setTitleColor:[UIColor colorWithRed:0/255.0 green:169/255.0 blue:130/255.0 alpha:1.0] forState:UIControlStateSelected];
            [button.titleLabel sizeToFit];
            self.indicatorView.width = 50;//KScreenWidth*120/375;
            self.indicatorView.centerXv  = button.centerXv;
        }
    }
    
    [titlesView addSubview:indicatorView];
    
    
}

#pragma mark -- 设置底部滚动视图
-(void)setContentView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame=CGRectMake(0, Height_NavBar+44, KScreenWidth, KScreenHeight-Height_NavBar-44);
    [scrollView setShowsHorizontalScrollIndicator:NO];
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.width *5, 0);
    [self.view insertSubview:scrollView atIndex:0];
    self.view.backgroundColor = rgb(241, 242, 243);
    if (@available(iOS 11.0,*)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.scrollView = scrollView;
    
    // 添加第一个控制器的view
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
}
-(void)titleClick:(UIButton *)button
{
    // 修改按钮状态
    self.currentSeletedButton.enabled = YES;
    button.enabled = NO;
    self.currentSeletedButton = button;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = 50;//KScreenWidth*120/375;
        self.indicatorView.centerXv = button.centerXv;
    }];
    
    // 滚动
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = button.tag *self.scrollView.width;
    [self.scrollView setContentOffset:offset animated:YES];
}
#pragma mark -- 添加子控制器
-(void)addSubControlelr
{
    //    CashViewController *vc = [[CashViewController alloc] init];
    MyOrderViewController *vc = [[MyOrderViewController alloc] init];
    vc.process = -1;//全部
    [self addChildViewController:vc];
    MyOrderViewController *vc1 = [[MyOrderViewController alloc] init];
    vc1.process = 1;//待支付
    [self addChildViewController:vc1];
    MyOrderViewController *vc2 = [[MyOrderViewController alloc] init];
    vc2.process = 2;//待发货
    [self addChildViewController:vc2];
    MyOrderViewController *vc3 = [[MyOrderViewController alloc] init];
    vc3.process = 3;//已发货
    [self addChildViewController:vc3];
    MyOrderViewController *vc4 = [[MyOrderViewController alloc] init];
    vc4.process = 4;//待评价
    [self addChildViewController:vc4];
    
}

#pragma mark -- <UIScrollerViewDelegate>
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    if ([scrollView isEqual: self.scrollView]) {
        // 当前索引
        NSInteger index = scrollView.contentOffset.x / scrollView.width;
        // 取出子控制器
        UIViewController *viewController = self.childViewControllers[index];
        viewController.view.xv = scrollView.contentOffset.x;
        viewController.view.yv = 0;
        viewController.view.height = scrollView.height;
        [scrollView addSubview:viewController.view];
    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isEqual: self.scrollView]) {
        [self scrollViewDidEndScrollingAnimation:scrollView];
        // 点击按钮
        NSInteger index = scrollView.contentOffset.x / scrollView.width;
        [self titleClick:self.titleView.subviews[index]];
    }
}



@end
